const { SERVER_ERR } = require("../config/error.js");
const blogsService = require("../service/blogs.service.js");
const settingService = require("../service/setting.service.js");

class TagController {
  async update(ctx, next) {
    try {
      const { aboutContent, focusContent } = ctx.request.body;
      const res = await settingService.update({
        aboutContent,
        focusContent,
      });
      ctx.body = {
        code: 200,
        message: "更新成功",
        data: res,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async info(ctx, next) {
    try {
      const res = await settingService.info();
      ctx.body = {
        code: 200,
        message: "查找成功",
        data: res,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async focusBlog(ctx, next) {
    try {
      const res1 = await settingService.info();
      const ids = res1.focusContent.split(";");
      const finData = await Promise.all(ids.map((id) => {
        return blogsService.detail(id);
      }))
      ctx.body = {
        code: 200,
        message: "查找成功",
        data: finData.filter((item) => item),
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
}

module.exports = new TagController();
